/*
 * @(#)TestsetTranslator.java
 *
 *
 *
 * ver 1.0 Sep 4, 2007 ggranum
 */
package org.testfire.realm.view.json.translator;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.testfire.realm.control.auth.user.UserDBAO;
import org.testfire.realm.control.exception.UserNotFoundException;
import org.testfire.realm.model.entities.User;
import org.testfire.realm.model.entities.testfire.CrontaskEntity;
import org.testfire.realm.model.entities.testfire.TestRunnerQueueEntity;
import org.testfire.realm.util.json.JsonField;
import org.testfire.realm.util.json.Tools;

/**
 * Application Scope
 *
 */
public class QueueTranslator implements JsonTranslator<TestRunnerQueueEntity>
{

    static final List<JsonField> ALL_FIELDS = new ArrayList<JsonField>();

    static
    {
        ALL_FIELDS.add( new JsonField( "id", JsonField.TYPE_INT ) );
        ALL_FIELDS.add( new JsonField( "name", JsonField.TYPE_STRING ) );
        ALL_FIELDS.add( new JsonField( "description", JsonField.TYPE_STRING ) );
        ALL_FIELDS.add( new JsonField( "userName", JsonField.TYPE_STRING ) );
        ALL_FIELDS.add( new JsonField( "useAnyAvailableAgent", JsonField.TYPE_BOOLEAN) );
        ALL_FIELDS.add( new JsonField( "cronTask", JsonField.TYPE_OBJECT ) );
    }

    public String toJson( TestRunnerQueueEntity object )
    {
        return Tools.getJsonString( object, ALL_FIELDS );
    }

    public String toJson( TestRunnerQueueEntity object, String fields ) throws JSONException
    {

        List<JsonField> jFields = JsonField.parse( fields );
        JSONObject obj = new JSONObject();
        for ( JsonField f : jFields )
        {

            if ( f.getName().equals( "cronTask" ) )
            {
                obj.put( "cronTask", object.getCronTask().getCronString() );
            }
            else if ( f.getName().equals( "name" ) )
            {
                obj.put( "name", object.getName() );
            }
            else if ( f.getName().equals( "description" ) )
            {
                obj.put( "description", object.getDescription() );
            }
            else if ( f.getName().equals( "id" ) )
            {
                obj.put( "id", object.getId() );
            }
            else if ( f.getName().equals( "userName" ) )
            {
                try
                {
                    User user = UserDBAO.getInstance().getUser( object.getUser() );
                    String userName = user != null ? user.getName() : "-unknown-";
                    obj.put( "userName", userName );
                }
                catch ( UserNotFoundException e )
                {
                    obj.put( "userName", "-unknown-" );
                }
            }
            else if ( f.getName().equals( "useAnyAvailableAgent" ) )
            {
                obj.put( "useAnyAvailableAgent", object.getUseAnyAvailableAgent() );
            }

        }
        return obj.toString();
    }

    public String toJson( List<TestRunnerQueueEntity> objects )
    {
        return Tools.getJsonString( objects, ALL_FIELDS );
    }

    public String toJsonArray( List<TestRunnerQueueEntity> objects, String fields ) throws JSONException
    {
        StringBuilder b = new StringBuilder( "[ " );

        for ( TestRunnerQueueEntity e : objects )
        {
            b.append( toJson( e, fields ) ).append( ", " );
        }
        if ( objects.size() > 0 )
        {
            b.delete( b.length() - 2, b.length() );
        }
        b.append( " ]" );
        return b.toString();
    }

    public TestRunnerQueueEntity toObject( String json ) throws JSONException
    {
        JSONObject obj = new JSONObject( json );
        TestRunnerQueueEntity queue = new TestRunnerQueueEntity();
        CrontaskEntity cronTask = new CrontaskEntity();

        // fields.
        int id = (Integer)obj.get( "id" );
        String cronString = (String)obj.get( "cronTask" );
        cronTask.setCronString( cronString );
        String name = (String)obj.get( "name" );
        Boolean useAnyAvailableAgent = (Boolean)obj.get( "useAnyAvailableAgent" );
        String description = (String)obj.get( "description" );

        queue.setId( id );
        queue.setCronTask( cronTask );
        queue.setDescription( description );
        queue.setName( name );
        queue.setUseAnyAvailableAgent( useAnyAvailableAgent );
        return queue;
    }

    public TestRunnerQueueEntity copyValues( TestRunnerQueueEntity to, TestRunnerQueueEntity from )
    {
        to.getCronTask().setCronString( from.getCronTask().getCronString() );
        to.setDescription( from.getDescription() );
        to.setName( from.getName() );
        to.setUseAnyAvailableAgent( from.getUseAnyAvailableAgent() );
        return to;
    }


}